import {post} from '../common/js/utils'

export const setUserInfo = (value = {}) => {
  return {
    type: 'USER_INFO',
    value
  }
}

export const asyncSetUserInfo = (cb) => {
  return dispatch => {
    post('/api/users/check').then(res => {
      if (res.result === 1) {
        let userInfo = Object.assign({status: 1}, res.data)
        dispatch(setUserInfo(userInfo))
      } else {
        dispatch(setUserInfo({status: -1}))
      }
      if (typeof cb === 'function') cb()
    }).catch(err => console.log(err))
  }
}
